function Snake (ele){
    //保存地图对象
 this.ele=ele
 //长度
 this.len=20
 //存蛇
 this.snakeObj = [
    { x: 3, y :2, color:'red', ele:null},  //蛇头
    { x: 2, y :2, color:'blue', ele:null}, // 蛇的身体
    { x: 1, y :2, color:'blue', ele:null}  // 蛇的身体

  ]
  this.direction='right'
}


Snake.prototype.remove=function(){
    for(let i=0;i<this.snakeObj.length;i++){
        if(this.snakeObj[i].ele){
            this.ele.removeChild(this.snakeObj[i].ele)
        }
    }
}

Snake.prototype.create=function(){
    this.remove()
    for(let i=0;i<this.snakeObj.length;i++){  //循环遍历蛇
    let divObj=document.createElement('div')   //创建蛇的标签对象
    //设置宽高颜色 坐标
    divObj.style.width= this.len+'px'
    divObj.style.height= this.len+'px'
    divObj.style.position='absolute' 
    divObj.style.left= this.snakeObj[i].x*this.len+'px'
    divObj.style.top= this.snakeObj[i].y*this.len+'px'
    divObj.style.background=this.snakeObj[i].color
    //单独保存下蛇节的对象
     this.snakeObj[i].ele=divObj
    //放到页面中展示
    this.ele.appendChild(divObj)
    }
}

Snake.prototype.move=function(){
    //将构造函数中的蛇的x数据加一
    for(let i=this.snakeObj.length-1;i>0;i--){
        this.snakeObj[i].x = this.snakeObj[i-1].x 
        this.snakeObj[i].y = this.snakeObj[i-1].y 
       }
       switch(this.direction){
           case'top':
           this.snakeObj[0].y -=1
           break;
           case'bottom':
           this.snakeObj[0].y +=1
           break;
           case'left':
           this.snakeObj[0].x -=1
           break;
           case'right':
           this.snakeObj[0].x +=1
           break;

       }    
       this.create()
}